package JDBC;

import java.nio.file.Path;
import java.sql.*;

public class Common {

    private static final String user = "root";
    private static final String password = "123456";
    private static final String jdbcurl = "jdbc:mysql://localhost:3306/test";
    private static final String Driver = "com.mysql.cj.jdbc.Driver";
    private Connection conn = null;
    private Statement stat = null;
    private ResultSet relt = null;

    //连接
        public void accept() {
        try {
            Class.forName(Driver);
             conn = DriverManager.getConnection(jdbcurl,user,password);
            if (!conn.isClosed()){
                System.out.println("数据库连接成功.........");
            }
        }catch (Exception e) {
            System.out.println("数据库连接失败...........");
        }
    }

    //连接时间
    public void acceptTime() {
        long start = System.currentTimeMillis();
        accept();
        long end = System.currentTimeMillis();
        long time = start - end;
        System.out.println("连接时间:"+time);
    }

    //关闭连接
    public void close() {
            try {
                this.conn.isClosed();
                System.out.println("关闭连接成功........");
            }catch (Exception e){
                System.out.println("关闭连接失败........");
            }
    }
    //关闭连接时间
    public void closeTime() {
            long start = System.currentTimeMillis();
            close();
            long end = System.currentTimeMillis();
            long time1 = end - start;
        System.out.println("关闭连接时间:"+time1);
    }

    //增
    public void insert(int id,String name,String age) {
            String sql = "insert into nonepool(id,name,age) values(?,?,?)";
            try {
                PreparedStatement preStmt=conn.prepareStatement(sql);
                preStmt.setInt(1, 1);
                preStmt.setString(2,name);
                preStmt.setString(3,age);
                preStmt.executeUpdate();
                System.out.println("数据插入成功.....");
                preStmt.close();
            }catch (Exception e){
                System.out.println("数据插入失败.....");
            }
    }
    //增的时间
    public void insertTime() {
            long start = System.currentTimeMillis();
            insert(1,"张三","16");
            long end = System.currentTimeMillis();
            long time = end - start;
        System.out.println("数据插入的时间:"+time);
    }

    //删
    public void drop(int id) {
            String sql = "delete from nonepool where id = ?";
            try {
                PreparedStatement preStmt =  conn.prepareStatement(sql);
                preStmt.setInt(1, id);
                preStmt.executeUpdate();
                preStmt.close();
                System.out.println("数据删除成功");
            }catch (Exception e){
                System.out.println("删除失败......");
            }
    }
    //删的时间
    public void dropTime() {
            long start = System.currentTimeMillis();
            drop(1);
            long end = System.currentTimeMillis();
            long time = end - start;
        System.out.println("删除的时间:"+time);
    }

    //查
    public void select() {
            String sql = "select *from nonepool";
            try {
                stat = conn.createStatement();
                relt = stat.executeQuery(sql);
                System.out.println("-----------------------------------");
                System.out.println("id"+"\t"+"name"+"\t"+"age");
                System.out.println("-----------------------------------");
                while (relt.next()) {
                    int uid = relt.getInt("id");
                    String uname = relt.getString("name");
                    String uage = relt.getString("age");
                    System.out.println(uid+"\t"+uname+"\t"+uage);
                    System.out.println("数据查询成功......");
                }
                stat.close();
            }catch (Exception e){
                System.out.println("查询失败......");
            }
    }
    //查的时间
    public void selectTime() {
            long start = System.currentTimeMillis();
            select();
            long end = System.currentTimeMillis();
            long time = end - start;
        System.out.println("查的时间:"+time);
    }

    //改
    public void updata(int id,String age){
            String sql = "update nonepool set age = ? where id = ?";;
            try {
                PreparedStatement preStmt = conn.prepareStatement(sql);
                preStmt.setString(1,"25");
                preStmt.setInt(2,1);
                preStmt.executeUpdate();
                System.out.println("数据修改成功.......");
                preStmt.close();
            }catch (Exception e){
                System.out.println("修改失败.........");
            }
    }
    public void updataTime() {
            long start = System.currentTimeMillis();
            updata(2,"25");
            long end = System.currentTimeMillis();
            long time = end - start;
        System.out.println("修改的时间:"+time);
    }
    public static void main(String[] args) {
            Common common = new Common();
            common.acceptTime();
            common.insertTime();
            common.selectTime();
            common.updataTime();
            common.dropTime();
            common.closeTime();
    }
}
